package com.rover022.tool.view
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	public class ImageItem extends Sprite
	{
		private var _imageGallery:ImageGallery = null;
		private var _url:String = null;
		private var _tfLabel:TextField = null;
		private var _content:DisplayObject = null;
		private var _contentWidth:Number = 0;
		private var _contentHeight:Number = 0;
		private var _loader:Loader = null;
		private var _loaded:Boolean = false;
		
		private static var _width:Number = 200;
		private static var _height:Number = 200;
		public static function get boundWidth():Number
		{
			return _width;
		}
		public static function get boundHeight():Number
		{
			return _height + 25;
		}
		
		public function ImageItem(imageGallery:ImageGallery)
		{
			super();
			_imageGallery = imageGallery;
			
			graphics.clear();
			graphics.beginFill(0xffb0b0a0, 1.0);
			graphics.drawRect(0, 0, _width, _height);
			graphics.endFill();
			
			_tfLabel = new TextField();
			_tfLabel.x = 0;
			_tfLabel.y = _height;
			_tfLabel.width = _width;
			_tfLabel.height = 20;
			_tfLabel.selectable = false;
			_tfLabel.autoSize = TextFieldAutoSize.CENTER;
			super.addChild(_tfLabel);
			
			_loaded = false;
		}
		
		public function dispose():void
		{
			super.removeChildren();
			if (_loader)
			{
				_loader.unload();
				_loader = null;
			}
			_content = null;
			_tfLabel = null;
			_url = null;
		}
		
		public function load(url:String):void
		{
			_url = decodeURIComponent(url);
			_tfLabel.text = _url.slice(_url.lastIndexOf("/") + 1);
			_loaded = false;
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
			var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
			_loader.load(new URLRequest(_url), context);
		}
		
		public function get url():String
		{
			return _url;
		}
		
		public override function get name():String
		{
			return _url.slice(_url.lastIndexOf("/") + 1);
		}
		
		public function get type():String
		{
			return _url.substr(_url.lastIndexOf(".") + 1).toLowerCase();
		}
		
		public function get contentWidth():Number
		{
			return _contentWidth;
		}
		
		public function get contentHeight():Number
		{
			return _contentHeight;
		}
		
		public function get content():DisplayObject
		{
			return _content;
		}
		
		private function onLoadComplete(evt:Event):void
		{
			_loaded = true;
			
			_content = _loader.content;
			var movieContent:MovieClip = _content as MovieClip;
			if (movieContent)
			{
				movieContent.mouseEnabled = false;
				movieContent.mouseChildren = false;
			}
			super.addChild(_content);
			
			var scaleRatio:Number = 1;
			_contentWidth = _content.width;
			_contentHeight = _content.height;
			if (_content.width / _content.height > _width / _height)
			{
				scaleRatio = _width / _content.width;
				_content.scaleX = scaleRatio;
				_content.scaleY = scaleRatio;
				_content.x = 0;
				_content.y = 0.5 * (_height - _content.height);
			}
			else
			{
				scaleRatio = _height / _content.height;
				_content.scaleX = scaleRatio;
				_content.scaleY = scaleRatio;
				_content.x = 0.5 * (_width - _content.width);
				_content.y = 0;
			}
			
			_tfLabel.text += (" " + _contentWidth + " × " + _contentHeight);

			_imageGallery.onItemLoaded(this);
		}
		
		public function get loaded():Boolean
		{
			return _loaded;
		}
		
		private function onLoadError(evt:IOErrorEvent):void
		{
			
		}
	}
}